<template>
  <!-- 订货计划表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="450px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="要货数量" prop="planQuantity">
        <el-input
          v-model="formData.planQuantity"
          placeholder="请输入计划采购数量"
          @change="planQuantityChange"
          clearable
          style="width: 250px"
          disabled
        />
      </el-form-item>
      <el-form-item label="可满足" prop="agreeQuantity">
        <el-input-number
          v-model="formData.agreeQuantity"
          :min="0"
          style="width: 250px"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定
        </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "SRMSRMPLANFEEDBACKADDORUPDATE",
  inheritAttrs: false,
});
let minBatchQuantity = ref(0);
let maxPlanQuantity = ref(0);
const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    planQuantityChange();
  }
);

const rules = reactive({
  purchasingCycleType: [],
  planQuantity: [],
  agreeQuantity: { required: true, message: "请输入数量" },
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  plantId: "",
  plantCode: "",
  plantName: "",
  purchasePlanNumber: "",
  partnerId: "",
  partnerCode: "",
  materialId: "",
  materialNumber: "",
  materialName: "",
  storageLocationId: "",
  storageLocationCode: "",
  storageLocationName: "",
  arriveDate: "",
  arriveYear: "",
  arriveMonth: "",
  arriveWeek: "",
  purchasingCycleType: "",
  planQuantity: 0,
  agreeQuantity: 0,
  unitCode: "",
  replyUserId: "",
  replyUserName: "",
  replyTime: "",
  replyContent: "",
  orderScheduleState: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        organizationId,
        organizationCode,
        organizationName,
        plantId,
        plantCode,
        plantName,
        purchasePlanNumber,
        partnerId,
        partnerCode,
        materialId,
        materialNumber,
        materialName,
        storageLocationId,
        storageLocationCode,
        storageLocationName,
        arriveDate,
        arriveYear,
        arriveMonth,
        arriveWeek,
        purchasingCycleType,
        planQuantity,
        agreeQuantity,
        unitCode,
        replyUserId,
        replyUserName,
        replyTime,
        replyContent,
        orderScheduleState,
        remarks,
        createTime,
        createdUserId,
        lastUpdateTime,
        lastUpdatedUserId,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        organizationId,
        organizationCode,
        organizationName,
        plantId,
        plantCode,
        plantName,
        purchasePlanNumber,
        partnerId,
        partnerCode,
        materialId,
        materialNumber,
        materialName,
        storageLocationId,
        storageLocationCode,
        storageLocationName,
        arriveDate,
        arriveYear,
        arriveMonth,
        arriveWeek,
        purchasingCycleType,
        planQuantity,
        agreeQuantity,
        unitCode,
        replyUserId,
        replyUserName,
        replyTime,
        replyContent,
        orderScheduleState,
        remarks,
        createTime,
        createdUserId,
        lastUpdateTime,
        lastUpdatedUserId,
        deleted,
      });
    }
  }
);

function planQuantityChange() {
  let planAgreement = JSON.parse(JSON.stringify(props.data));
  formData.agreeQuantity = planAgreement.planQuantity;
  // if (planAgreement.purchasePlanNumber != undefined) {
  //   getPurchasePlanNumbersInfo(planAgreement.purchasePlanNumber);
  // }
}
//根据协议号查询协议，暂时不需要
async function getPurchasePlanNumbersInfo(number: string) {
  request({
    url: `/srm/core/srmorderschedule/plan/${number}`,
    method: "get",
  }).then((data) => {
    // console.log(data.data)
    minBatchQuantity.value = data.data.minBatchQuantity;
    maxPlanQuantity.value = data.data.maxPlanQuantity;
  });
}

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.organizationName = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.plantName = "";
  formData.purchasePlanNumber = "";
  formData.partnerId = "";
  formData.partnerCode = "";
  formData.materialId = "";
  formData.materialNumber = "";
  formData.materialName = "";
  formData.storageLocationId = "";
  formData.storageLocationCode = "";
  formData.storageLocationName = "";
  formData.arriveDate = "";
  formData.arriveYear = "";
  formData.arriveMonth = "";
  formData.arriveWeek = "";
  formData.purchasingCycleType = "";
  formData.planQuantity = 0;
  formData.agreeQuantity = 0;
  formData.unitCode = "";
  formData.replyUserId = "";
  formData.replyUserName = "";
  formData.replyTime = "";
  formData.replyContent = "";
  formData.orderScheduleState = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 订货计划保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "反馈" : "新增";
      await request({
        url: "/srm/partner/srmorderfeedback/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
